package com.cqc.albumdemo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import java.util.List;

/**
 * Created by ${cqc} on 2017/4/6.
 */

public class MyAdapter extends RecyclerView.Adapter {

    private List<String> pathList ;
    private int itemSize;

    public MyAdapter(List<String> pathList,int itemSize) {
        this.pathList = pathList;
        this.itemSize = itemSize;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.item, parent, false);
        return new MyViewHolder(itemView,itemSize);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        MyViewHolder holder = (MyViewHolder) viewHolder;

        Glide.with(holder.itemView.getContext()).load(pathList.get(position)).into(holder.iv);
        //如果设置了回调，则调用点击事件
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(v,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return pathList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{// implements View.OnClickListener

        public ImageView iv;
        public int itemSize;

        public MyViewHolder(View itemView,int itemSize) {
            super(itemView);
            this.itemSize = itemSize;
            itemView.getLayoutParams().height = itemSize;
            itemView.requestLayout();
            iv = (ImageView) itemView.findViewById(R.id.iv);
        }
    }

    // 点击回调
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
